package edu.cuc.learningAkka.chap2;

import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import org.junit.Test;
import scala.concurrent.Future;

import java.util.concurrent.*;

import static akka.pattern.Patterns.ask;
import static org.junit.Assert.assertEquals;
import static scala.compat.java8.FutureConverters.*;

/**
 * Created by bingruiqi on 2017/9/10.
 */
public class PongActorTest {
    ActorSystem system = ActorSystem.create();
    ActorRef actorRef = system.actorOf(Props.create(JavaPongActor.class));

    @Test
    public void shouldReplyToPingWithPong() throws InterruptedException, ExecutionException, TimeoutException {
        Future future = ask(actorRef, "ping", 1000);
        final CompletionStage<String> completionStage  = toJava(future);
        final CompletableFuture<String> completableFuture = (CompletableFuture<String>) completionStage;
        assertEquals("Pong", completableFuture.get(1000, TimeUnit.MILLISECONDS));
    }
}
